home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap22 / exer2202 / NumberServer.java < prev   
Encoding:
Java Source  |  1997-04-20  |  4.5 KB  |  169 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.awt.*;
  4.  
  5. /**
  6.  * NumberServer -- The server for a number guessing application.
  7.  *
  8.  * To run as a server, name the server as the first command line 
  9.  * argument:
  10.  *   java NumberServer bluehorse.com
  11.  * or
  12.  *   java NumberServer local          // to run locally
  13.  */
  14.  
  15. public class NumberServer {
  16.     private int              port = 5001;    // The default port.
  17.     private boolean          listening = true;
  18.  
  19.     public static void main(String args[]) {
  20.        new NumberServer().server();
  21.     }
  22.    
  23.     // As a server, we create a server socket bound to the specified
  24.     // port, wait for a connection, and then spawn a thread to 
  25.     // read data coming in over the network via the socket.
  26.  
  27.     private void server() {
  28.         ServerSocket serverSock = null;
  29.  
  30.         try {
  31.            serverSock = new ServerSocket(port, 50);
  32.         } catch (IOException x) {
  33.            System.out.println(x.getMessage() +
  34.                ": Failed to create server socket.");
  35.            System.exit(1);
  36.         }
  37.  
  38.         while (listening) {
  39.            try {
  40.  
  41.               Socket sock = serverSock.accept();
  42.               new HandleGuesses(sock).start();
  43.        
  44.            } catch (IOException x) {
  45.               System.out.println(x.getMessage() +
  46.                  ": Failed to connect to client.");
  47.               System.exit(1);
  48.            }
  49.         } 
  50.  
  51.         // At this point, we don't need the ServerSocket anymore.
  52.         if (serverSock != null) {
  53.            try {
  54.                serverSock.close();
  55.            } catch (IOException x) {
  56.            }           
  57.         }        
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. /*
  65.  * HandleGuesses communicates with the client.
  66.  */
  67. class HandleGuesses extends Thread {
  68.     private Socket sock;
  69.     private DataInputStream remoteIn;
  70.     private DataOutputStream remoteOut;
  71.     private boolean listening = true;
  72.     private int num = (int)(Math.random() * 10);
  73.  
  74.     public HandleGuesses(Socket sock) throws IOException {
  75.         this.sock = sock;
  76.         remoteIn = new DataInputStream(sock.getInputStream());
  77.         remoteOut = new DataOutputStream(sock.getOutputStream());
  78.     }
  79.  
  80.     public synchronized void run() {
  81.         String s;
  82.         String op;
  83.         String guessString;
  84.         int guessInt;
  85.         try {
  86.  
  87.             while (listening) {
  88.                 s = remoteIn.readUTF();
  89.                 op = s.substring(0, 1);
  90.                 guessString = s.substring(1, 2);
  91.                 guessInt = new Integer(guessString).intValue();
  92.  
  93.                 if (op.equals(">"))
  94.                    handleGreaterThan(guessInt);
  95.                 else if (op.equals("<"))
  96.                    handleLessThan(guessInt);
  97.                 else
  98.                    handleEquals(guessInt);
  99.             }
  100.  
  101.         } catch (NumberFormatException x) {
  102.            System.out.println(x.getMessage() +
  103.                ": Protocol problem: expected a number.");
  104.  
  105.         } catch (IOException x) {
  106.             System.out.println(x.getMessage() +
  107.                ": Connection to peer lost.");
  108.  
  109.         } finally {
  110.            try {
  111.               if (remoteIn != null) {
  112.                  remoteIn.close();
  113.                  remoteIn = null;
  114.               }
  115.  
  116.               if (remoteOut != null) {
  117.                  remoteOut.close();
  118.                  remoteOut = null;
  119.               }
  120.  
  121.               if (sock != null) {
  122.                  sock.close();
  123.                  sock = null;
  124.               }
  125.  
  126.            } catch (IOException x) {
  127.            }
  128.         }
  129.     }
  130.  
  131.     private void handleGreaterThan(int i) throws IOException {
  132.         if (num > i)
  133.            remoteOut.writeUTF("true");
  134.         else
  135.            remoteOut.writeUTF("false");
  136.     }
  137.  
  138.     private void handleLessThan(int i) throws IOException{
  139.         if (num < i)
  140.            remoteOut.writeUTF("true");
  141.         else
  142.            remoteOut.writeUTF("false");
  143.     }
  144.  
  145.     private void handleEquals(int i) throws IOException {
  146.         if (i == num) {
  147.            remoteOut.writeUTF("You guessed it!");
  148.            listening = false;
  149.         }
  150.  
  151.         else
  152.            remoteOut.writeUTF("false");
  153.     }
  154.  
  155.     protected void finalize() throws Throwable {
  156.         try {
  157.            if (remoteIn != null)
  158.               remoteIn.close();
  159.            if (remoteOut != null)
  160.               remoteOut.close();
  161.            if (sock != null)
  162.               sock.close();
  163.         } catch (IOException x) {
  164.         }
  165.         super.finalize();
  166.     }
  167.  
  168. }
  169.